from xdbSearcher import XdbSearcher
from flask_cors import CORS
from flask import Flask,abort
import os
import ipaddress
app = Flask(__name__)
CORS(app)

searcher = XdbSearcher(contentBuff=XdbSearcher.loadContentFromFile(os.path.dirname(os.path.abspath(__file__)) + "/data/ip2region.xdb")) 
@app.get("/search/<ip>")
def search(ip):
    try:
        ipaddress.ip_address(ip)
        return {
            "ip":ip,
            "region":searcher.search(ip),
        }
    except:
        abort(400,f"{ip} is not an ip.")